package com.lemurian.bankdata.action.business;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lemurian.bankdata.lite.logger.BankDataLogger;
import com.lemurian.bankdata.lite.utils.FormatData;
import com.lemurian.bankdata.lite.utils.GeneralConstant;
import com.lemurian.bankdata.report.dto.RekapSertifikat;
import com.lemurian.bankdata.services.ManagementService;
import com.lemurian.bankdata.utils.BaseAction;
import com.opensymphony.xwork2.Preparable;

public class RekapitulasiSertifikatMasukAction extends BaseAction implements Preparable {

	private static final long serialVersionUID = 1L;
	
	private BankDataLogger log = BankDataLogger.getLogger(RekapitulasiSertifikatMasukAction.class.getSimpleName());
	
	private String startTime="";
    private String endTime="";
    
    private List<RekapSertifikat> belumTtd;
	private List<RekapSertifikat> sudahTtd;

	private static final String FILE_LOCATION = ServletActionContext.getServletContext().getRealPath("jasper/rekap_jumlah_sertifikat_masuk.jasper");
	private static final String IMAGE_LOCATION = ServletActionContext.getServletContext().getRealPath("");
	
	// download
	private String downloadContentType;
	private InputStream inputStream;
	private String downloadFileName;
	
	private Map parameters;
	
	@Autowired
	private ManagementService managementService;
	
	@Override
	public void prepare() throws Exception {
		
	}
	
	@Override
	public String list() {
		setPreviousPage(GeneralConstant.LIST);
		try {
			Date _startDate = null;
			Date _endDate = null;
			if(startTime == null && endTime !=null && startTime.length() > 0) {
				_endDate = FormatData.dateFormatShow.parse(endTime.trim());
                log.sInfo("Search criteria : endTime <= %s", _endDate.toString());
            } else if(startTime != null && endTime ==null && endTime.length() > 0) {
            	_startDate = FormatData.datetimeFormatHM.parse(startTime.trim());
                log.sInfo("Search criteria : startTime >= %s", _endDate.toString());
            } else if(startTime != null && endTime !=null && startTime.length() > 0 && endTime.length() > 0) {
            	_endDate = FormatData.dateFormatShow.parse(endTime.trim());
            	_startDate = FormatData.datetimeFormatHM.parse(startTime.trim());
                log.sInfo("Search criteria : startTime >= %s", startTime.toString());
                log.sInfo("Search criteria : endTime <= %s", endTime.toString());
            }
			
			belumTtd = managementService.summaryReport(_startDate, _endDate, false);
			sudahTtd = managementService.summaryReport(_startDate, _endDate, true);
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		setMappedRequest(GeneralConstant.LIST);
		return SUCCESS;
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String pdf() {
		
		try {
			parameters = new HashMap();
			JRBeanCollectionDataSource dataSource = null;
			
			// Sample Data
			RekapSertifikat data = null;
			List<RekapSertifikat> belomTtd = new ArrayList<RekapSertifikat>();
			data = new RekapSertifikat();data.setKodeUrusan("0");data.setJumlah(30);belomTtd.add(data);
			data = new RekapSertifikat();data.setKodeUrusan("1");data.setJumlah(40);belomTtd.add(data);
			data = new RekapSertifikat();data.setKodeUrusan("402");data.setJumlah(10);belomTtd.add(data);
			
			dataSource = new JRBeanCollectionDataSource(belomTtd);
			parameters.put("belumttd", dataSource);
			
			List<RekapSertifikat> sudahTtd = new ArrayList<RekapSertifikat>();
			data = new RekapSertifikat();data.setKodeUrusan("401");data.setJumlah(100);belomTtd.add(data);
			data = new RekapSertifikat();data.setKodeUrusan("403");data.setJumlah(20);belomTtd.add(data);
			data = new RekapSertifikat();data.setKodeUrusan("1");data.setJumlah(25);belomTtd.add(data);
			
			dataSource = new JRBeanCollectionDataSource(sudahTtd);
			parameters.put("belumttd", dataSource);
			
			parameters.put("image", IMAGE_LOCATION);
			parameters.put("tanggalAwal", "-");
			parameters.put("tanggalAkhir", "-");
			
			JasperReport jasperReport = (JasperReport) JRLoader.loadObject(FILE_LOCATION);
			
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
			
			byte[] pdfStream = JasperExportManager.exportReportToPdf(jasperPrint);
			
			inputStream = new ByteArrayInputStream(pdfStream);
			downloadContentType = "application/vnd.pdf";
			downloadFileName = "Pencocok_Trx_Hari_Ini.pdf";
			
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		return "download";
	}
	
	public String excel() {
		return "download";
	}
	
	@Override
	public String add() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String detail() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDownloadContentType() {
		return downloadContentType;
	}

	public void setDownloadContentType(String downloadContentType) {
		this.downloadContentType = downloadContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getDownloadFileName() {
		return downloadFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	public List<RekapSertifikat> getBelumTtd() {
		return belumTtd;
	}

	public void setBelumTtd(List<RekapSertifikat> belumTtd) {
		this.belumTtd = belumTtd;
	}

	public List<RekapSertifikat> getSudahTtd() {
		return sudahTtd;
	}

	public void setSudahTtd(List<RekapSertifikat> sudahTtd) {
		this.sudahTtd = sudahTtd;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
}